package cn.terenas.tool.excel.common.dto.data;

import cn.terenas.tool.excel.common.constant.Constant;
import cn.terenas.tool.excel.common.enums.result.TransferTypeEnum;
import cn.terenas.tool.excel.config.Config;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Optional;

@Data
@AllArgsConstructor
public class Transfer {

    String sourceAirportCode;

    String targetAirportCode;

    String entryFlightCode;

    String departureFlightCode;

    int firstClassNtc;

    int firstClassTc;

    int businessClassNtc;

    int businessClassTc;

    int superEconomyClassNtc;

    int superEconomyClassTc;

    int economyClassNtc;

    int economyClassTc;

    int economyClassChildNtc;

    int economyClassChildTc;


    public int getTotalNtc() {
        return this.firstClassNtc + this.businessClassNtc +
                this.superEconomyClassNtc + this.economyClassNtc + this.economyClassChildNtc;
    }

    public int getTotalTc() {
        return this.firstClassTc + this.businessClassTc +
                this.superEconomyClassTc + this.economyClassTc + this.economyClassChildTc;
    }


    public TransferTypeEnum getTransferType() {
        if (Config.isDomesticAirport(this.sourceAirportCode) && Config.isDomesticAirport(targetAirportCode)) {
            return TransferTypeEnum.DD;
        } else if (Config.isDomesticAirport(this.sourceAirportCode) && Config.isInternationalAirport(targetAirportCode)) {
            return TransferTypeEnum.DI;
        } else if (Config.isInternationalAirport(this.sourceAirportCode) && Config.isDomesticAirport(targetAirportCode)) {
            return TransferTypeEnum.ID;
        } else if (Config.isInternationalAirport(this.sourceAirportCode) && Config.isInternationalAirport(targetAirportCode)) {
            return TransferTypeEnum.II;
        } else {
            throw new RuntimeException("转机类型错误!");
        }
    }

    public String getRouteName() {
        // 机场名称不存在时直接使用机场编码.
        return Optional.of(Constant.CODE_MAPPING.get(this.sourceAirportCode)).orElse(this.sourceAirportCode) + "——" +
                Optional.of(Constant.CODE_MAPPING.get(this.targetAirportCode)).orElse(this.targetAirportCode);
    }

    public String getUnionCode() {
        return Optional.ofNullable(this.departureFlightCode)
                .map(flight -> flight.substring(0, 2))
                .map(String::toUpperCase)
                .orElseThrow(() -> new RuntimeException("航班\"" + this.departureFlightCode + "\"联盟编码抽取失败!"));
    }

    @Override
    public String toString() {
        return "转机数据:{" +
                "来源机场编码:'" + sourceAirportCode + '\'' +
                ", 目的机场编码:'" + targetAirportCode + '\'' +
                ", 入港航班:'" + entryFlightCode + '\'' +
                ", 离港航班:'" + departureFlightCode + '\'' +
                ", 头等舱NTC:" + firstClassNtc +
                ", 头等舱TC:" + firstClassTc +
                ", 商务舱NTC:" + businessClassNtc +
                ", 商务舱TC:" + businessClassTc +
                ", 超级经济舱NTC:" + superEconomyClassNtc +
                ", 超级经济舱TC:" + superEconomyClassTc +
                ", 经济舱NTC:" + economyClassNtc +
                ", 经济舱TC:" + economyClassTc +
                '}';
    }

}
